Html中img控件的src属性

您所在的位置:网站首页 html中的img src属性 Html中img控件的src属性

Html中img控件的src属性

2023-10-07 09:45| 来源: 网络整理| 查看: 265

方式一:

直接给src属性一个服务器端资源的地址,img控件会自动获取并解析资源。

方式二:

当访问的资源不在工程路径下时,想到的办法是通过返回给img控件字节流,让img控件解析,开始直接将ajax异步获取的字节流设给了img的src属性,发现无法显示图片,通过http://blog.csdn.net/doulinxu/article/details/59636710的指点,完成通过给src属性赋予字节数据来显示数据,大致为:字节流先在服务器端进行Base64编码,在输出前需要将MIME的字符串字节输出,代码如下:

@RequestMapping(value="viewDirect") public void viewDirect(@RequestParam String url,HttpServletResponse response) throws Exception { String filePath = baseDir + "/" + url; File file = new File(filePath); if (file.exists()&&file.isFile()) { String suffix = url.substring(url.lastIndexOf("."), url.length()); if (suffix==null) { return; } String metaType = ""; if (suffix.equalsIgnoreCase("jpg")||suffix.equalsIgnoreCase("jpeg")||suffix.equalsIgnoreCase("jpe")) { metaType = "data:image/jpeg;base64,"; }else if (suffix.equalsIgnoreCase("gif")) { metaType = "data:image/gif;base64,"; }else if (suffix.equalsIgnoreCase("bmp")) { metaType = "data:image/bmp;base64,"; }else { metaType = "data:image/jpeg;base64,";//默认解码方式,不合适 } InputStream inputStream = new FileInputStream(file); ServletOutputStream outputStream = response.getOutputStream(); // outputStream.write(metaType.getBytes()); // int length = -1; // byte[] buffer = new byte[1024]; // while ((length=inputStream.read(buffer, 0, 1024))!=-1) { // byte[] buf; // if (length


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3